<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ShopLevelController extends Controller
{
    protected $api_url = [
        'index' => ['get', 'shop/level'],
        'create'=> ['post', 'shop/level/add'],
        'update'   => ['put', 'shop/level/%d'],
        'delete'   => ['delete', 'shop/level/%d']
    ];

    protected $api_host;
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_backend');
    }

    public function indexAction()
    {
        $page = $this->request->get('page', 1);

        $params = [
            'page' => $page
        ];
        
        $ret = http_admin($this->api_url['index'])->send($params);

        $data = [];
        if (isset($ret['code']) && $ret['code'] === 0 && !empty($ret['data'])) {
            $data = $ret['data'];
        }

        return view('Admin.shop.level', paginate($this->request, $data));
    }

    public function createAction(){
        $this->validate($this->request, [
            'lv_ico_tag'=> 'required',
            'lv_txt_tag'=> 'required',
            'lv_min_grow'=>'required|integer',
            'lv_max_grow'=>'required|integer',
        ],[
            'lv_ico_tag'=> '请选择店铺标识',
            'lv_txt_tag'=> '请输入文字标识',
            'lv_min_grow'=>'请输入最低成长值|最低成长值必须为数字',
            'lv_max_grow'=>'请输入最高成长值|最高成长值必须为数字',
        ]);
        $data = $this->request->all([]);

        $ret = http_admin($this->api_url['create'])->send($data);

        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }

    public function updateAction($id){

        $data = $this->request->all([]);

        $ret = http_admin($this->api_url['update'],[$id])->send($data);

        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }

    public function deleteAction($id){

        $ret = http_admin($this->api_url['delete'],[$id])->send();

        if(isset($ret['code']) && $ret['code']===0){
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
}